package com.fieldchain.ex.fa100.ui.market.adapter;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.Comparator;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/17
 */
public class MarketCompare implements Comparator {
    private String method;
    private int type;

    public MarketCompare(String method,int type) {
        this.method = method;
        this.type=type;
        Log.e("type_mk22:",type+"");
    }

    @Override
    public int compare(Object a, Object b) {
        int cpt=0;
        String str1 = getResult(a, method);
        String str2 = getResult(b, method);
        if (method.equals("getMarket")) {
             cpt= str1.compareTo(str2);
        } else {
            float f1= Float.parseFloat(str1);
            float f2= Float.parseFloat(str2);
           if(f1==f2){
               cpt=0;
           }else if(f1>f2){
               cpt=1;
           }else if(f1<f2){
               cpt=-1;
           }
        }
        Log.e("sort3:",type+ "    "+cpt);
        if(type!=0){
            cpt=-cpt;
        }

        return cpt;
    }

    public String getResult(Object a, String name) {
        try {
            Class<?> obj = Class.forName(a.getClass().getName());
            Method method = obj.getMethod(name);
            String result = method.invoke(a).toString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "0";
    }

}
